{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 无监督学习章节\n",
    "# PCA\n",
    "from sklearn.decomposition import PCA # 导入主成分分析模块\n",
    "from sklearn.datasets import load_iris # 导入鸢尾花数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.68412563,  0.31939725],\n",
       "       [-2.71414169, -0.17700123],\n",
       "       [-2.88899057, -0.14494943],\n",
       "       [-2.74534286, -0.31829898],\n",
       "       [-2.72871654,  0.32675451],\n",
       "       [-2.28085963,  0.74133045],\n",
       "       [-2.82053775, -0.08946138],\n",
       "       [-2.62614497,  0.16338496],\n",
       "       [-2.88638273, -0.57831175],\n",
       "       [-2.6727558 , -0.11377425],\n",
       "       [-2.50694709,  0.6450689 ],\n",
       "       [-2.61275523,  0.01472994],\n",
       "       [-2.78610927, -0.235112  ],\n",
       "       [-3.22380374, -0.51139459],\n",
       "       [-2.64475039,  1.17876464],\n",
       "       [-2.38603903,  1.33806233],\n",
       "       [-2.62352788,  0.81067951],\n",
       "       [-2.64829671,  0.31184914],\n",
       "       [-2.19982032,  0.87283904],\n",
       "       [-2.5879864 ,  0.51356031],\n",
       "       [-2.31025622,  0.39134594],\n",
       "       [-2.54370523,  0.43299606],\n",
       "       [-3.21593942,  0.13346807],\n",
       "       [-2.30273318,  0.09870885],\n",
       "       [-2.35575405, -0.03728186],\n",
       "       [-2.50666891, -0.14601688],\n",
       "       [-2.46882007,  0.13095149],\n",
       "       [-2.56231991,  0.36771886],\n",
       "       [-2.63953472,  0.31203998],\n",
       "       [-2.63198939, -0.19696122],\n",
       "       [-2.58739848, -0.20431849],\n",
       "       [-2.4099325 ,  0.41092426],\n",
       "       [-2.64886233,  0.81336382],\n",
       "       [-2.59873675,  1.09314576],\n",
       "       [-2.63692688, -0.12132235],\n",
       "       [-2.86624165,  0.06936447],\n",
       "       [-2.62523805,  0.59937002],\n",
       "       [-2.80068412,  0.26864374],\n",
       "       [-2.98050204, -0.48795834],\n",
       "       [-2.59000631,  0.22904384],\n",
       "       [-2.77010243,  0.26352753],\n",
       "       [-2.84936871, -0.94096057],\n",
       "       [-2.99740655, -0.34192606],\n",
       "       [-2.40561449,  0.18887143],\n",
       "       [-2.20948924,  0.43666314],\n",
       "       [-2.71445143, -0.2502082 ],\n",
       "       [-2.53814826,  0.50377114],\n",
       "       [-2.83946217, -0.22794557],\n",
       "       [-2.54308575,  0.57941002],\n",
       "       [-2.70335978,  0.10770608],\n",
       "       [ 1.28482569,  0.68516047],\n",
       "       [ 0.93248853,  0.31833364],\n",
       "       [ 1.46430232,  0.50426282],\n",
       "       [ 0.18331772, -0.82795901],\n",
       "       [ 1.08810326,  0.07459068],\n",
       "       [ 0.64166908, -0.41824687],\n",
       "       [ 1.09506066,  0.28346827],\n",
       "       [-0.74912267, -1.00489096],\n",
       "       [ 1.04413183,  0.2283619 ],\n",
       "       [-0.0087454 , -0.72308191],\n",
       "       [-0.50784088, -1.26597119],\n",
       "       [ 0.51169856, -0.10398124],\n",
       "       [ 0.26497651, -0.55003646],\n",
       "       [ 0.98493451, -0.12481785],\n",
       "       [-0.17392537, -0.25485421],\n",
       "       [ 0.92786078,  0.46717949],\n",
       "       [ 0.66028376, -0.35296967],\n",
       "       [ 0.23610499, -0.33361077],\n",
       "       [ 0.94473373, -0.54314555],\n",
       "       [ 0.04522698, -0.58383438],\n",
       "       [ 1.11628318, -0.08461685],\n",
       "       [ 0.35788842, -0.06892503],\n",
       "       [ 1.29818388, -0.32778731],\n",
       "       [ 0.92172892, -0.18273779],\n",
       "       [ 0.71485333,  0.14905594],\n",
       "       [ 0.90017437,  0.32850447],\n",
       "       [ 1.33202444,  0.24444088],\n",
       "       [ 1.55780216,  0.26749545],\n",
       "       [ 0.81329065, -0.1633503 ],\n",
       "       [-0.30558378, -0.36826219],\n",
       "       [-0.06812649, -0.70517213],\n",
       "       [-0.18962247, -0.68028676],\n",
       "       [ 0.13642871, -0.31403244],\n",
       "       [ 1.38002644, -0.42095429],\n",
       "       [ 0.58800644, -0.48428742],\n",
       "       [ 0.80685831,  0.19418231],\n",
       "       [ 1.22069088,  0.40761959],\n",
       "       [ 0.81509524, -0.37203706],\n",
       "       [ 0.24595768, -0.2685244 ],\n",
       "       [ 0.16641322, -0.68192672],\n",
       "       [ 0.46480029, -0.67071154],\n",
       "       [ 0.8908152 , -0.03446444],\n",
       "       [ 0.23054802, -0.40438585],\n",
       "       [-0.70453176, -1.01224823],\n",
       "       [ 0.35698149, -0.50491009],\n",
       "       [ 0.33193448, -0.21265468],\n",
       "       [ 0.37621565, -0.29321893],\n",
       "       [ 0.64257601,  0.01773819],\n",
       "       [-0.90646986, -0.75609337],\n",
       "       [ 0.29900084, -0.34889781],\n",
       "       [ 2.53119273, -0.00984911],\n",
       "       [ 1.41523588, -0.57491635],\n",
       "       [ 2.61667602,  0.34390315],\n",
       "       [ 1.97153105, -0.1797279 ],\n",
       "       [ 2.35000592, -0.04026095],\n",
       "       [ 3.39703874,  0.55083667],\n",
       "       [ 0.52123224, -1.19275873],\n",
       "       [ 2.93258707,  0.3555    ],\n",
       "       [ 2.32122882, -0.2438315 ],\n",
       "       [ 2.91675097,  0.78279195],\n",
       "       [ 1.66177415,  0.24222841],\n",
       "       [ 1.80340195, -0.21563762],\n",
       "       [ 2.1655918 ,  0.21627559],\n",
       "       [ 1.34616358, -0.77681835],\n",
       "       [ 1.58592822, -0.53964071],\n",
       "       [ 1.90445637,  0.11925069],\n",
       "       [ 1.94968906,  0.04194326],\n",
       "       [ 3.48705536,  1.17573933],\n",
       "       [ 3.79564542,  0.25732297],\n",
       "       [ 1.30079171, -0.76114964],\n",
       "       [ 2.42781791,  0.37819601],\n",
       "       [ 1.19900111, -0.60609153],\n",
       "       [ 3.49992004,  0.4606741 ],\n",
       "       [ 1.38876613, -0.20439933],\n",
       "       [ 2.2754305 ,  0.33499061],\n",
       "       [ 2.61409047,  0.56090136],\n",
       "       [ 1.25850816, -0.17970479],\n",
       "       [ 1.29113206, -0.11666865],\n",
       "       [ 2.12360872, -0.20972948],\n",
       "       [ 2.38800302,  0.4646398 ],\n",
       "       [ 2.84167278,  0.37526917],\n",
       "       [ 3.23067366,  1.37416509],\n",
       "       [ 2.15943764, -0.21727758],\n",
       "       [ 1.44416124, -0.14341341],\n",
       "       [ 1.78129481, -0.49990168],\n",
       "       [ 3.07649993,  0.68808568],\n",
       "       [ 2.14424331,  0.1400642 ],\n",
       "       [ 1.90509815,  0.04930053],\n",
       "       [ 1.16932634, -0.16499026],\n",
       "       [ 2.10761114,  0.37228787],\n",
       "       [ 2.31415471,  0.18365128],\n",
       "       [ 1.9222678 ,  0.40920347],\n",
       "       [ 1.41523588, -0.57491635],\n",
       "       [ 2.56301338,  0.2778626 ],\n",
       "       [ 2.41874618,  0.3047982 ],\n",
       "       [ 1.94410979,  0.1875323 ],\n",
       "       [ 1.52716661, -0.37531698],\n",
       "       [ 1.76434572,  0.07885885],\n",
       "       [ 1.90094161,  0.11662796],\n",
       "       [ 1.39018886, -0.28266094]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = load_iris() # 加载鸢尾花数据集\n",
    "n_components = 2 # 设置主成分的数量\n",
    "model = PCA(n_components=n_components) # 创建PCA对象\n",
    "model = model.fit(data.data) # 拟合数据\n",
    "model.transform(data.data) # 将数据转换为主成分空间"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
